Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

File Handling in java → Create directory

File Handling in java

Create directory

Directory handling in Java

Java provides functionalities to manage directories (folders) through the File class and other helper classes. These functionalities include creating, deleting, listing contents, and checking directory existence.

1. Creating Directories

Using mkdir() The mkdir() method of the File class attempts to create a new directory at the specified path.
Creating directory File myDir = new File("new_directory"); if (myDir.mkdir()) { System.out.println("Directory created successfully!"); } else { System.out.println("Directory creation failed."); }
Explanation: File myDir = new File("new_directory");: This line creates a File object representing the desired directory path "new_directory". myDir.mkdir(): This method attempts to create the directory.

2. Creating Nested Directories

Using mkdirs() The mkdirs() method of the File class is similar to mkdir() but can create nested directories if they don't already exist.
Creating nested directories File myDir = new File("parent/new_directory/sub_directory"); if (myDir.mkdirs()) { System.out.println("Directory structure created successfully!"); } else { System.out.println("Directory creation failed."); }
Explanation: File myDir = new File("parent/new_directory/sub_directory");: This line creates a File object representing the path with nested directories. myDir.mkdirs(): This method attempts to create all necessary directories in the path if they don't exist.

3. Deleting Directories

Using delete() The delete() method of the File class can be used to delete directories. However, the directory must be empty for successful deletion.
Deleting directories File myDir = new File("empty_directory"); if (myDir.delete()) { System.out.println("Directory deleted successfully!"); } else { System.out.println("Directory deletion failed (might not be empty)."); }
Explanation: File myDir = new File("empty_directory");: This line creates a File object representing the directory to delete. myDir.delete(): This method attempts to delete the directory.

4. Checking Directory Existence

Using exists() The exists() method of the File class returns true if the file or directory represented by the File object exists.
Checking if folder exists or not File myDir = new File("existing_directory"); if (myDir.exists()) { System.out.println("Directory exists!"); } else { System.out.println("Directory does not exist."); }
Explanation: File myDir = new File("existing_directory");: This line creates a File object representing the directory to check. myDir.exists(): This method checks if the directory exists.

5. Listing Directory Contents

Using listFiles() The listFiles() method of the File class returns an array of File objects representing the files and subdirectories within the directory.
listing files in folder File myDir = new File("parent_directory"); if (myDir.isDirectory()) { File[] files = myDir.listFiles(); if (files != null) { for (File file : files) { System.out.println(file.getName()); // Print file/subdirectory name } } else { System.out.println("Directory is empty."); } } else { System.out.println("Specified path is not a directory."); }
Explanation: File myDir = new File("parent_directory");: This line creates a File object representing the directory to list contents. myDir.isDirectory(): This check ensures the path represents a directory. myDir.listFiles(): This method returns an array of File objects for the directory contents. loop:The loop iterates through the files array and prints the name of each file or subdirectory using file.getName().

Tutorials